/*
 * Copyright (C) 2004, Velocityme Partnership
 * OrganisationActions.java
 *
 * Created on 05 October 2004, 05:20
 */

package com.velocityme.www.interfaces.nodeactions;

import com.velocityme.interfaces.*;
import com.velocityme.valueobjects.*;
import com.velocityme.www.actionforms.nodeactionforms.OrganisationValueActionForm;
import javax.servlet.http.*;
import org.apache.struts.action.*;

/**
 *
 * @author  Robert
 */
public class OrganisationActions extends ContactableActions {
    
    /** Creates a new instance of OrganisationActions */
    public OrganisationActions() {
    }
    
    public ActionForward show(ActionMapping mapping, 
                              ActionForm form,
                              HttpServletRequest request,
                              HttpServletResponse response) throws Exception {
        NodePK nodePK = (NodePK)request.getAttribute("nodePK");
        OrganisationPK organisationPK = new OrganisationPK(nodePK.getNodeId());
        HttpSession session = request.getSession();
        KeySession key = (KeySession)session.getAttribute("sessionKey");
        RemoteClientSession cs = RemoteClientSessionUtil.getHome().create();
        session.setAttribute("organisationValue", cs.getOrganisationValue(key, organisationPK));
        return mapping.findForward("ACTION_DISPLAY");
    }
    
    public ActionForward showCreate(ActionMapping mapping, 
                                    ActionForm form,
                                    HttpServletRequest request,
                                    HttpServletResponse response) throws Exception {
        NodePK nodePK = (NodePK)request.getAttribute("nodePK");
        OrganisationPK organisationPK = new OrganisationPK(nodePK.getNodeId());
        HttpSession session = request.getSession();
        KeySession key = (KeySession)session.getAttribute("sessionKey");
        RemoteClientSession cs = RemoteClientSessionUtil.getHome().create();
        session.setAttribute("parentNodePK", nodePK);
        OrganisationValueActionForm organisationValueActionForm = new OrganisationValueActionForm();
        createContactDetails(key, cs, organisationValueActionForm);
        session.setAttribute("organisationActionForm", organisationValueActionForm);
        OrganisationValue organisationValue = new OrganisationValue();
        organisationValue.setContactableValue(new ContactableValue());
        organisationValue.getContactableValue().setNodeValue(new NodeValue());
        session.setAttribute("organisationValue", organisationValue);
        return mapping.findForward("ACTION_EDIT");
    }
    
    public ActionForward showEdit(ActionMapping mapping, 
                                  ActionForm form,
                                  HttpServletRequest request,
                                  HttpServletResponse response) throws Exception {
        NodePK nodePK = (NodePK)request.getAttribute("nodePK");
        OrganisationPK organisationPK = new OrganisationPK(nodePK.getNodeId());
        HttpSession session = request.getSession();
        KeySession key = (KeySession)session.getAttribute("sessionKey");
        RemoteClientSession cs = RemoteClientSessionUtil.getHome().create();
        OrganisationValue organisationValue = cs.getOrganisationValue(key, organisationPK);
        session.setAttribute("organisationValue", organisationValue);
        OrganisationValueActionForm organisationValueActionForm = new OrganisationValueActionForm();
        organisationValueActionForm.setName(organisationValue.getContactableValue().getNodeValue().getName());
        organisationValueActionForm.setDescription(organisationValue.getContactableValue().getNodeValue().getDescription());
        editContactDetails(key, cs, organisationValue.getContactableValue(), organisationValueActionForm);
        session.setAttribute("organisationActionForm", organisationValueActionForm);
        return mapping.findForward("ACTION_EDIT");
    }
    
    public ActionForward edit(ActionMapping mapping, 
                              ActionForm form,
                              HttpServletRequest request,
                              HttpServletResponse response) throws Exception {
        HttpSession session = request.getSession();
        KeySession key = (KeySession)session.getAttribute("sessionKey");
        RemoteClientSession cs = RemoteClientSessionUtil.getHome().create();
        NodePK nodePK;
        if (request.getParameter("btnSubmit").equals("OK")) {
            OrganisationValue organisationValue = (OrganisationValue)session.getAttribute("organisationValue");
            OrganisationValueActionForm organisationValueActionForm = (OrganisationValueActionForm)form;
            organisationValue.getContactableValue().getNodeValue().setName(organisationValueActionForm.getName());
            organisationValue.getContactableValue().getNodeValue().setDescription(organisationValueActionForm.getDescription());
            organisationValue.getContactableValue().setContactDetailValues(organisationValueActionForm.getContactDetailValues(cs.getAllContactDetailValues(key)));
            OrganisationPK organisationPK;
            if (organisationValue.organisationIdHasBeenSet()) {
                organisationPK = organisationValue.getPrimaryKey();
                cs.organisationEdit(key, organisationPK, organisationValue);
            }
            else {
                ActionErrors errors = new ActionErrors();
                NodePK parentNodePK = (NodePK)session.getAttribute("parentNodePK");
                organisationPK = cs.organisationCreate(key, parentNodePK, organisationValue);
            }
            nodePK = new NodePK(organisationPK.getOrganisationId());
        }
        // Cancel simply displays the original node again
        else {
            nodePK = new NodePK((Integer)session.getAttribute("nodeId"));
        }
        request.setAttribute("nodePK", nodePK);
        return mapping.findForward("ACTION_SUCCESS");
    }
    
}
